package testemodulorede;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Chat implements Runnable {

    JTextArea incoming;
    JTextField outgoing;
    BufferedReader reader;
    GamePanel game;
    String player;
    String avatar;

    public Chat( GamePanel _game, String _player, String _avatar ){

        game = _game;
        player = _player;
        avatar = _avatar;

    }

    public void incomingMessage(String _message){

        System.out.println("read:" + _message);
        incoming.append( _message.substring(9) + "\n");

    }

    public void run(){

        JFrame frame = new JFrame("FlexShooter's Chat");
        JPanel mainPanel = new JPanel();
        incoming = new JTextArea(15,50);
        incoming.setLineWrap(true);
        incoming.setWrapStyleWord(true);
        incoming.setEditable(false);
        JScrollPane qScroller = new JScrollPane(incoming);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        outgoing = new JTextField(20);
        JButton sendButton = new JButton("Send");
        sendButton.addActionListener( new SendButtonListener() );
        mainPanel.add(qScroller);
        mainPanel.add(outgoing);
        mainPanel.add(sendButton);

        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        frame.setSize(400, 500);
        frame.pack();
        frame.setVisible(true);
    }

    public class SendButtonListener implements ActionListener{

        public void actionPerformed(ActionEvent ev){

            String temp = player + " (" + avatar + ") 8=D :" + outgoing.getText();
            game.sendPackage("8:CHAT:" + temp);
            outgoing.setText("");
            outgoing.requestFocus();
           
            
        }

    }

}
